package com.lhk.appframework.manager;


import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.entity.StringEntity;
import android.content.Context;
import com.lhk.appframework.constants.ConstantNet;
import com.lhk.appframework.net.NetRequestCallback;
import com.lhk.appframework.net.packet.NetRequestPacket;
import com.lhk.appframework.net.packet.NetResponsePacket;
import com.lhk.appframework.utils.json.UtilsJson;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;

public class ImplManagerHttpAsynhttp extends BaseManager implements ManagerHttp {
	
	private AsyncHttpClient client;
	
	
	private static ImplManagerHttpAsynhttp instance = null;
	private ImplManagerHttpAsynhttp() {
		
	}
	
	public static ImplManagerHttpAsynhttp getInstance(){
		if(instance == null){
			synchronized (ImplManagerHttpAsynhttp.class) {
				if(instance == null){
					instance = new ImplManagerHttpAsynhttp();
				}
			}
		}
		return instance;
	}
	
	
	
	@Override
	public void post(Context context, String reqUrl, NetRequestPacket req, NetRequestCallback callback) {
		String jsonReq = UtilsJson.object2Json(req);
		
		HttpEntity entity = null;
		try {
			entity = new StringEntity(jsonReq, ConstantNet.ENCODING);
			client = getAsyncHttpClient();
			client.post(context, reqUrl, entity, ConstantNet.CONTENT_TYPE, new MAsyncHttpResponseHandler(callback));
		} catch (Exception e) {
			
			e.printStackTrace();
			
			NetResponsePacket res = NetResponsePacket.newNetResponse();
			res.getHeader().setStatusCode(ConstantNet.RESPONSE_CODE_ERROR);
			res.getHeader().setMsgCode(ConstantNet.RESPONSE_CODE_ERROR_MSG);
			callback.onFailure(res);
		}
		
	}
	
	
	
	
	private class  MAsyncHttpResponseHandler extends AsyncHttpResponseHandler{

		private NetRequestCallback callback;
		
		
		public MAsyncHttpResponseHandler(NetRequestCallback callback) {
			this.callback = callback;
		}
		
		@Override
		public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
			
			if(statusCode == 200){
				try {
					String result = new String(responseBody, ConstantNet.ENCODING);
					NetResponsePacket res = NetResponsePacket.newNetResponse(result);
					if(res.getHeader().getStatusCode() == ConstantNet.RESPONSE_CODE_SUCCESS){
						callback.onSuccess(res);
						return ;
					}
				} catch (Exception e) {
					e.printStackTrace();
					NetResponsePacket res = NetResponsePacket.newNetResponse();
					res.getHeader().setStatusCode(ConstantNet.RESPONSE_CODE_ERROR_603);
					res.getHeader().setMsgCode(ConstantNet.RESPONSE_CODE_ERROR_603_MSG);
					callback.onFailure(res);
				}
			}else{
				NetResponsePacket res = NetResponsePacket.newNetResponse();
				res.getHeader().setStatusCode(ConstantNet.RESPONSE_CODE_ERROR_699);
				res.getHeader().setMsgCode(ConstantNet.RESPONSE_CODE_ERROR_699_MSG);
				
				callback.onFailure(res);
			}
		
		}
		
		@Override
		public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
			NetResponsePacket res = NetResponsePacket.newNetResponse();
			res.getHeader().setStatusCode(ConstantNet.RESPONSE_CODE_ERROR_699);
			res.getHeader().setMsgCode(ConstantNet.RESPONSE_CODE_ERROR_699_MSG);
			callback.onFailure(res);
		}
	}
	
	
	
	private AsyncHttpClient getAsyncHttpClient(){
		if (client == null) {
			synchronized (ImplManagerHttpAsynhttp.class) {
				if (client == null) {
					client = new AsyncHttpClient();
					client.setTimeout( 5 * 1000);
				}
			}
		}
		return client;
	}
}
